#include "communicator.h"

#include <QBuffer>
#include <QDataStream>

namespace Avd {

Communicator::Communicator() {
	messages = new QSharedMemory("avd");
}

Communicator::~Communicator() {
	if(messages) {
		messages->unlock();
		delete messages;
	}
}

void Communicator::fileDownloaded(const File& file) throw(CommunicatorError) {
	// load into shared memory
	QBuffer buffer;
	buffer.open(QBuffer::ReadWrite);
	QDataStream out(&buffer);
	out << ("0" + file.getFullPath());
	int size = buffer.size();

	messages->create(size);

	messages->lock();

	char *to = (char*)messages->data();
	const char *from = buffer.data().data();
	memcpy(to, from, qMin(messages->size(), size));

	messages->unlock();
}

void Communicator::fileCreated(const File& file) throw(CommunicatorError) {

}

void Communicator::fileUploaded(const File& file) throw(CommunicatorError) {

}

void Communicator::fileRenamed(const File& oldFile, const File& newFile) throw(CommunicatorError) {

}


}

